﻿Imports System.Drawing

Public Class SerialPort
    Implements UCBase.I_BinaryDataExchange

    Implements UCBase.I_LoadSave
    Implements UCBase.I_CloseNotify

    Private M_Port As String = ""
    Private Sub SerialPort_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Led1.State = UCBase.Led.LedState.Led_Off
        Timer1.Enabled = True
    End Sub

    Private Sub SerialPort_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Resize
        '135, 89
        Me.Width = 135
        Me.Height = 80
    End Sub
    Public Property Port() As String
        Get
            Return M_Port
        End Get
        Set(ByVal value As String)
            M_Port = value
            SetPort()
        End Set
    End Property
    Private Sub SetPort()
        If SerialPort1.IsOpen Then
            SerialPort1.Close()
            Led1.State = UCBase.Led.LedState.Led_Off
        End If
        SerialPort1.PortName = M_Port
        SerialPort1.Open()
        If SerialPort1.BytesToRead > 0 Then
            SerialPort1.DiscardInBuffer()
        End If
        Led1.State = UCBase.Led.LedState.Led_On
        Label1.Text = M_Port
    End Sub
    Public Sub Write(ByVal data() As Byte) Implements UCBase.I_BinaryDataExchange.Write
        If SerialPort1.IsOpen = False Then SetPort()
        SerialPort1.Write(data, 0, data.Length)
    End Sub

    Public ReadOnly Property BytesToRead() As Integer Implements UCBase.I_BinaryDataExchange.BytesToRead
        Get
            If SerialPort1.IsOpen = False Then SetPort()
            Return SerialPort1.BytesToRead
        End Get
    End Property

    Public ReadOnly Property BytesToWrite() As Integer Implements UCBase.I_BinaryDataExchange.BytesToWrite
        Get
            If SerialPort1.IsOpen = False Then SetPort()
            Return SerialPort1.BytesToWrite
        End Get
    End Property

    Public Function Read() As Byte() Implements UCBase.I_BinaryDataExchange.Read
        If SerialPort1.IsOpen = False Then SetPort()
        If SerialPort1.BytesToRead > 0 Then
            Dim OutData(SerialPort1.BytesToRead - 1) As Byte
            SerialPort1.Read(OutData, 0, SerialPort1.BytesToRead)
            Return OutData
        End If
        Return Nothing
    End Function

    Public Sub ClearBuffers() Implements UCBase.I_BinaryDataExchange.ClearBuffers
        If SerialPort1.IsOpen = False Then SetPort()
        SerialPort1.DiscardInBuffer()
        SerialPort1.DiscardOutBuffer()
    End Sub

    Public Function GetSettings() As String Implements UCBase.I_LoadSave.GetSettings
        Return SerialPort1.PortName
    End Function

    Public Sub SetSettings(ByVal Settings As String) Implements UCBase.I_LoadSave.SetSettings
        M_Port = Settings
        SetPort()
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        If SerialPort1.IsOpen = False Then
            SetPort()
        Else
            Led1.State = UCBase.Led.LedState.Led_On
            Timer1.Enabled = False
        End If
    End Sub

    Public Sub ShutDown() Implements UCBase.I_CloseNotify.ShutDown
        SerialPort1.Close()
    End Sub

    Public Function Read1(ByVal Count As Integer) As Byte() Implements UCBase.I_BinaryDataExchange.Read
        Return Nothing
    End Function
End Class
